import { getFlowData } from '@/api/businessFlow.js'
import { getPageInfo, getBaseInfo, deleteInvoice, getInvoiceInfo } from '@/api/vmsManualInvoicing.js'
import { homeProcess } from '@/api/wfProcessBusiness.js'
import { processLog ,getNextHandleInfo} from '@/api/businessFlow.js'
import Vue from 'vue'


const invoice = {
    namespaced: true,
    state: {
        invoiceFlowStatus:{
            "busType": "", // FLOW_SGKP
            "busId": "", // 流程id
        },
        invoinceFormStart1:{},
        busData:{
            fppz: '01',
            hzqrdqrjg:"Y"
        },
        invoinceFormInfo2: {
            "records": [],
            "total": 0,
            "size": 10,
            "current": 1,
            "pages": 0
        },
        invoinceFormTrack4: [],
        invoinceFormBaseInfo5:{
            lclx:'专票', 
            fqrq:'2023年12月25日15:38:36', 
            fqr:'1', 
            lcbh:'999999',

        },
        invoinceFormAdvice6:{
            nextNodeName:'',
            nextHandleUser:'',
            handleResult:'1',
            handleUserList:[],
            remark: '',

        },

        newInvoiceInfo:{

        },


        homeParams:{}
    },
    mutations: {
        changeInvoinceFormStart1(state, val) {
            state.invoinceFormStart1 = val;
            if(val.newAddinitParams){
                if(Vue.prototype.hasAuth('VMS_FULL_MANUAL_01')){
                    val.newAddinitParams.fppz = '01'
                }else if(Vue.prototype.hasAuth('VMS_FULL_MANUAL_02')){
                    val.newAddinitParams.fppz = '02'
                }else{
                    val.newAddinitParams.fppz = ''
                }
                state.busData = val.newAddinitParams
            }else{
                let toObjBusData;
                try {
                    toObjBusData = JSON.parse(val.busData)
                } catch (error) {
                    console.log(error);
                    toObjBusData = {
                        hzqrdqrjg:"Y"
                    }
                }
                state.busData = toObjBusData
            }

        },
        changeInvoinceFormTwo(state, val) {
            state.invoinceFormInfo2 = val;
        },
        resetFormTwo(state){
            state.invoinceFormInfo2 = {
                "records": [],
                "total": 0,
                "size": 10,
                "current": 1,
                "pages": 0
            }
        },
        changeInvoinceFormFour(state, val) {
            state.invoinceFormTrack4 = val || [];
        },
        changeInvoinceFormSix(state, val) {
            state.invoinceFormAdvice6 = {
                ...val,
                remark :'',
                handleResult :'1',
            }
            try {
                state.invoinceFormAdvice6.nextHandleUser = val.handleUserList[0].userCode
            } catch (error) {

            }
        },
        newInvoiceInfoFn(state, val) {
            state.newInvoiceInfo = val
        },

    },
    actions: {
        async getFirst({ commit, state,dispatch }, payload) {
            return new Promise(async (resolve,reject)=>{
                let { data, code, message } = await getFlowData(
                    {
                        "busType":  payload.busType , 
                        "busId": payload.busId
                    }
                )
                if (code == 200) {
                    if(payload.newAddinitParams){
                        data.newAddinitParams = payload.newAddinitParams
                    }
                    commit('changeInvoinceFormStart1', data)
                    setTimeout(()=>{
                        resolve(data)
                        // if(data.busId)dispatch('getTwo')
                    })
                }
            })
        },
        async getTwo({ commit, state }, payload ={}) {
                let { data, code, message } = await getPageInfo(
                    {
                        "applyId":payload.busId ||  state.invoinceFormStart1.busId,
                        "pageNo": payload.current || state.invoinceFormInfo2.current,
                        "pageSize": payload.size || state.invoinceFormInfo2.size,
                    }
                )

                if (code == 200) {
                    commit('changeInvoinceFormTwo', data)
                }
        },
        async getFour({ commit, state }, busId) {
                let { data, code, message } = await processLog( {busId } )

                if (code == 200) {
                    commit('changeInvoinceFormFour', data)
                }
        },
        async getSix({ commit, state }, busId) {
                let { data, code, message } = await getNextHandleInfo(
                    {busId}
                )
                if (code == 200) {
                    commit('changeInvoinceFormSix', data)
                }
        },
        async addInvoice({ commit, state }) {
                return new Promise(async (resolve,reject)=>{
                    let { data, code, message } = await getBaseInfo()
                    if (code == 200) {
                        commit('newInvoiceInfoFn', data)
                        resolve(data)
                    }
                })
        },
        async delInvoiceInfoList({dispatch},row){
            return new Promise( async (resolve,reject)=>{
                let { data, code, message } = await deleteInvoice(
                    {
                        baseId:row.id
                    }
                )
                if (code == 200) {
                    dispatch('getTwo')
                    resolve(data)
                }
            })
        },
        async editInvoiceInfo({dispatch},row){
            return new Promise( async (resolve,reject)=>{
                let { data, code, message } = await getInvoiceInfo(
                    {
                        baseId:row.id
                    }
                )
                if (code == 200) {

                    resolve(data)
                }
            })
        },
        async homeGetList({dispatch,state},params){
            if(params)state.homeParams = params;

            return new Promise( async (resolve,reject)=>{
                let data = await homeProcess(
                    state.homeParams
                )
                if (data.code == 200) {
                    resolve(data)
                }
            })
        },

    },
}

export default invoice
